package com.zmt.content.api;

import com.zmt.content.dto.CourseBaseInfoDto;
import com.zmt.content.dto.CoursePreviewDto;
import com.zmt.content.dto.TeachplanDto;
import com.zmt.content.po.CoursePublish;
import com.zmt.content.service.CourseBaseService;
import com.zmt.content.service.CoursePublishService;
import com.zmt.content.service.TeachPlanService;
import com.zmt.content.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Api(value = "课程发布管理接口",tags = "课程发布管理接口")
@Controller
public class CoursePublishController {

    @Autowired
    private CoursePublishService coursePublishService;

    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private TeachPlanService teachPlanService;

    @ApiOperation("预览页面查看")
    @GetMapping("/coursepreview/{courseId}")
    public ModelAndView preview(@PathVariable("courseId") Long courseId){
        ModelAndView modelAndView = new ModelAndView();
        CoursePreviewDto coursePreviewInfo = coursePublishService.findCoursePreviewInfo(courseId);
        //有点小瑕疵，如果没有收费规则前端页面展示乱码
        modelAndView.addObject("model",coursePreviewInfo);
        modelAndView.setViewName("course_template");//指定模板名称
        return modelAndView;
    }

    @ApiOperation("提交审核")
    @ResponseBody//作用是将返回的对象，转化成json格式后放入Response的body中
    @PostMapping("/courseaudit/commit/{courseId}")
    public void commitAudit(@PathVariable("courseId") Long courseId){
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        Long companyId = Long.valueOf(user.getCompanyId());
        coursePublishService.commitAudit(companyId,courseId);
    }

    @ApiOperation("课程发布")
    @ResponseBody
    @PostMapping("/coursepublish/{courseId}")
    public void coursePublish(@PathVariable("courseId") Long courseId){
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        Long companyId = Long.valueOf(user.getCompanyId());
        coursePublishService.coursePublish(companyId,courseId);
    }

    @ApiOperation("查询课程发布信息")
    @ResponseBody
    @GetMapping("/r/coursepublish/{courseId}")
    public CoursePublish getCoursePublic(@PathVariable Long courseId){
        CoursePublish coursePublic = coursePublishService.getCoursePublic(courseId);
        return coursePublic;
    }

    @ResponseBody
    @GetMapping("/course/whole/{courseId}")
    public CoursePreviewDto getCourseBaseInfo(@PathVariable("courseId") Long courseId){
        CoursePreviewDto coursePreviewDto = new CoursePreviewDto();
        CourseBaseInfoDto courseBaseInfo = courseBaseService.getCourseBaseInfo(courseId);
        List<TeachplanDto> teachplanDtos = teachPlanService.selectTreeNodes(courseId);

        coursePreviewDto.setTeachplans(teachplanDtos);
        coursePreviewDto.setCourseBase(courseBaseInfo);
        return coursePreviewDto;
    }

}
